<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!-- 添加角色 -->
      <el-row>
        <el-col><el-button type="primary">添加角色</el-button></el-col>
      </el-row>

      <!-- 角色列表表格区域 -->
      <el-table :data="rolesListData" style="width: 100%" border>
        <!-- 展开行 -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <!-- 第一层 -->
            <el-row
              v-for="item1 in scope.row.children"
              :key="item1.id"
              class="row-item1"
            >
              <el-col :span="5">
                <el-tag
                  closable
                  @close="deleteRolesRight(scope.row, item1.id)"
                  >{{ item1.authName }}</el-tag
                >
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <!-- 第二层 -->
                <el-row
                  v-for="item2 in item1.children"
                  :key="item2.id"
                  class="row-item2"
                >
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      closable
                      @close="deleteRolesRight(scope.row, item2.id)"
                      >{{ item2.authName }}</el-tag
                    >
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <!-- 第三层 -->
                  <el-col :span="18">
                    <el-tag
                      type="warning"
                      v-for="item3 in item2.children"
                      :key="item3.id"
                      class="tag-item3"
                      closable
                      @close="deleteRolesRight(scope.row, item3.id)"
                      >{{ item3.authName }}</el-tag
                    >
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <!-- 编辑 -->
            <el-button type="primary" icon="el-icon-edit" size="mini"
              >编辑</el-button
            >
            <!-- 删除 -->
            <el-button type="danger" icon="el-icon-delete" size="mini"
              >删除</el-button
            >
            <!-- 分配权限 -->
            <el-button
              type="warning"
              icon="el-icon-setting"
              size="mini"
              @click="assignRightsBtn(scope.row)"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分配权限对话框 -->
    <el-dialog
      title="分配权限"
      :visible.sync="rolesAssignRightsBtnDialogVisible"
      width="50%"
      @close="rolesAssignRightsBtnDialogVisibleClose"
    >
      <!-- tree树状显示权限区域 -->
      <el-tree
        :data="rightsListData"
        :props="rightsListDefaultProps"
        node-key="id"
        :default-checked-keys="defaultExpandedKeys"
        show-checkbox
        default-expand-all
        ref="rightsTreeRef"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="rolesAssignRightsBtnDialogVisible = false"
          >取 消</el-button
        >
        <el-button type="primary" @click="assignRights">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 角色列表数据
      rolesListData: [],
      // 控制分配权限对话框显示与隐藏
      rolesAssignRightsBtnDialogVisible: false,
      // tree树状数据
      rightsListData: [],
      // tree树状配置
      rightsListDefaultProps: {
        label: 'authName',
        children: 'children'
      },
      // 默认展开的节点key的数组
      defaultExpandedKeys: [],
      // 角色ID
      roleId: ''
    }
  },
  created() {
    this.getRolesLise()
  },
  methods: {
    // 获取角色列表数据
    async getRolesLise() {
      const { data: res } = await this.$http.get('roles')
      this.rolesListData = res.data
      console.log(this.rolesListData)
    },
    // 分配权限按钮
    async assignRightsBtn(row) {
      const { data: res } = await this.$http.get('rights/tree')
      //   console.log(res)
      this.rightsListData = res.data
      // 将当前的角色ID保存为私有数据
      this.roleId = row.id
      this.getTreeLastID(row, this.defaultExpandedKeys)
      // 显示分配权限对话框
      this.rolesAssignRightsBtnDialogVisible = true
    },
    // 获取tree树状最后一层的ID
    getTreeLastID(row, arr) {
      if (!row.children) {
        return arr.push(row.id)
      }

      row.children.forEach(item => {
        return this.getTreeLastID(item, arr)
      })
    },
    // 关闭分类权限对话框的回调函数
    rolesAssignRightsBtnDialogVisibleClose() {
      // 默认展开的节点key的数组设置为[]
      this.defaultExpandedKeys = []
    },
    // 分配权限
    async assignRights() {
      console.log(this.roleId)
      // 目前选中的节点
      const key1 = this.$refs.rightsTreeRef.getCheckedKeys()
      // 目前半选中的节点
      const key2 = this.$refs.rightsTreeRef.getHalfCheckedKeys()
      // 把数组转换成以,分割的字符串
      const arrStr = [...key1, ...key2].join(',')
      const { data: res } = await this.$http.post(
        `roles/${this.roleId}/rights`,
        {
          rids: arrStr
        }
      )
      console.log(res)
      if (res.meta.status !== 200) return this.$message(res.meta.msg)
      this.$message.success(res.meta.msg)
      // 关闭分配权限对话框
      this.rolesAssignRightsBtnDialogVisible = false
      // 重新发送请求刷新页面
      this.getRolesLise()
    },
    // 删除角色指定权限
    async deleteRolesRight(roleId, RightId) {
      const resConfirm = await this.$confirm(
        '此操作将永久删除该权限, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(error => error)
      if (resConfirm !== 'confirm') return this.$message.info('已取消删除')
      const { data: res } = await this.$http.delete(
        `roles/${roleId.id}/rights/${RightId}`
      )
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      roleId.children = res.data
    }
  }
}
</script>

<style lang="less" scoped>
.row-item1,
.row-item2 {
  display: flex;
  align-items: center;
  padding-left: 10px;
}
.row-item1 {
  border-top: 1px solid #eee;
  &:last-child {
    border-bottom: 1px solid #eee;
  }
  .row-item2 {
    border-bottom: 1px solid #eee;
    &:last-child {
      border-bottom: none;
    }
  }
  .tag-item3 {
    margin: 5px;
  }
}

// icon图标
.el-icon-caret-right {
  margin-left: 5px;
}
</style>
